package com.example.demo.aop;

import testJdk.anno.older.AuthorAnnotation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author keyer
 * @Description : 实现自己的注解
 * Created by cyd on 2021-04-29 15:00:08
 */
@Component
@Aspect
public class AnnoAspect {
    /**
     * 扫描注解类所在的位置，定义切入点
     */
    @Pointcut("@annotation(testJdk.anno.older.AuthorAnnotation)")
    public void authorAnnotationCut() {
    }

    /**
     * 配置注解的切面拦截器
     * 使用前置拦截
     *
     * @param joinPoint  切入点对象，能获得方法签名（这个参数可以根据实际情况决定是否添加）
     * @param annotation 注解对象，用于传入注解的参数（这个参数可以根据实际情况决定是否添加）
     */
    @Before("authorAnnotationCut()&& @annotation(annotation)")
    public void before(JoinPoint joinPoint, AuthorAnnotation annotation) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.print("接受方法：" + method.getName() + " 前置日志");
        System.out.println("----------------------------------------------------");
        System.out.println("company:" + annotation.company());
        System.out.println("name:" + annotation.name());
        System.out.println("anno-type:" + annotation.annotationType());
        System.out.println("----------------------------------------------------");
    }
}
